package com.hzp.tool.common.ui.frame;

import com.hzp.tool.common.util.ConfigUtils;

import javax.swing.*;
import java.util.Properties;

public class FrameProperties {
	private Class<?> clazz;
	private Properties properties;

	public FrameProperties(JFrame frame) {
		this.clazz = frame.getClass();
		reload();
	}

	public void reload() {
		this.properties = ConfigUtils.readConfig(this.clazz);
	}

	public void save() {
		ConfigUtils.saveConfig(this.clazz, properties);
	}

	public void setValue(String key, Object value) {
		if (value == null) {
			properties.remove(key);
		} else {
			properties.setProperty(key, String.valueOf(value));
		}
	}

	public void removeValue(String key) {
		properties.remove(key);
	}

	public String getString(String key, String deft) {
		String value = properties.getProperty(key);
		return value == null ? deft : value;
	}

	public String getString(String key) {
		return properties.getProperty(key);
	}

	public Integer getInteger(String key, int deft) {
		String value = properties.getProperty(key);
		return value == null ? deft : Integer.valueOf(value);
	}

	public Integer getInteger(String key) {
		return getInteger(key, 0);
	}

	public Long getLong(String key, long deft) {
		String value = properties.getProperty(key);
		return value == null ? deft : Long.valueOf(value);
	}

	public Long getLong(String key) {
		return getLong(key, 0L);
	}

	public Float getFloat(String key, float deft) {
		String value = properties.getProperty(key);
		return value == null ? deft : Float.valueOf(value);
	}

	public Float getFloat(String key) {
		return getFloat(key, 0.0f);
	}

	public Double getDouble(String key, double deft) {
		String value = properties.getProperty(key);
		return value == null ? deft : Double.valueOf(value);
	}

	public Double getDouble(String key) {
		return getDouble(key, 0.0);
	}
}
